/**
  * Copyright (c) CSIRO Australia, 2009
  *
  * @author $Author: jia020 $
  * @version $Id: GoogleMapPanel.java 643 2009-05-20 00:20:55Z jia020 $
  */
package au.csiro.nt.pdsp.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;
import com.gwtext.client.util.JavaScriptObjectHelper;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.Viewport;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.AnchorLayoutData;
import com.gwtext.client.widgets.map.GoogleMap;
import com.gwtext.client.widgets.map.LatLonPoint;
import com.gwtext.client.widgets.map.MapPanel;
import com.gwtext.client.widgets.map.Marker;

/**
 * GoogleMapPanel create googlemap panel according to the XML data source. 
 *
 */
public class GoogleMapPanel extends Panel {

	// Panel viewportPanel;
	private MapPanel mapPanel;
	// Panel eastPanel;
	private 	String address = "marsfield nsw australia";
	private 	TextField addressField;
	private 	ToolbarButton refreshMapButton;
	/**
	 * Constructor to create googlemap panel according to the XML data source. 
	 * @param xmlData XML data source.
	 */	
	public GoogleMapPanel(String xmlData) {

		mapPanel = new GoogleMap();
		mapPanel.setTitle("[http://pdsp.nt.csiro.au]");
		mapPanel.setWidth(600);
		mapPanel.setHeight(500);
		mapPanel.addLargeControls();
		final GoogleMapPanel thisModule = this;

		com.google.gwt.xml.client.Document doc = XMLParser.parse(xmlData);

		NodeList records = doc.getElementsByTagName(PdspConst.XMLTAG_RECORD);
		int size = records.getLength();
		for (int i = 0; i < size; i++) {
			Element recordElement = (Element) records.item(i); 
			String address = recordElement.getElementsByTagName("residentialaddress").item(0).getFirstChild().getNodeValue();
			String firstname = recordElement.getElementsByTagName("firstname").item(0).getFirstChild().getNodeValue();
			String lastname = recordElement.getElementsByTagName("lastname").item(0).getFirstChild().getNodeValue();
			String centrelinkid = recordElement.getElementsByTagName("centrelinkid").item(0).getFirstChild().getNodeValue();
			String label = centrelinkid + " " + firstname + " " + lastname + " " + address;
			updateMap(address, label, JavaScriptObjectHelper.createObject(), thisModule);
		}
		this.add(mapPanel,new AnchorLayoutData("100%"));

	}
	/**
	 * update tag according the address 
	 * @param locationAddress address.
	 * @param label label of tag.
	 * @param llp javascriptobject
	 * @param thisModule GoogleMapPanel
	 */	
	public native void updateMap(String locationAddress, String label,
			JavaScriptObject llp, GoogleMapPanel thisModule) /*-{
		var geo = new $wnd.GClientGeocoder();

		geo.getLocations(locationAddress, 
			function(response) 
			{
				if (!response || response.Status.code != 200) 
				{
		 				alert("Unable to geocode that address");
				} 
				else 
		    		{
		    		var place = response.Placemark[0];
		    		llp.lat = place.Point.coordinates[1];
		    		llp.lon = place.Point.coordinates[0];		 
		    		llp.label = label;   		
		    		thisModule.@au.csiro.nt.pdsp.client.GoogleMapPanel::renderMap(Lcom/google/gwt/core/client/JavaScriptObject;)(llp);
		    		}
		   		}
		   	);
	}-*/;

	public void renderMap(JavaScriptObject jsObj) {
		double lat = Double.parseDouble(JavaScriptObjectHelper.getAttribute(jsObj,
				"lat"));
		double lon = Double.parseDouble(JavaScriptObjectHelper.getAttribute(jsObj,
				"lon"));
		String label = JavaScriptObjectHelper.getAttribute(jsObj, "label");
		LatLonPoint latLonPoint = new LatLonPoint(lat, lon);
		mapPanel.setCenterAndZoom(latLonPoint, 12);
		// mapPanel.addMarker(new Marker(latLonPoint));
		Marker marker = new Marker(latLonPoint);
		marker.setInfoBubble(label);
		mapPanel.addMarker(marker);
	}
	/**
	 * get bogus Addresses.
	 */	
	private static String[] getAddresses() {
		return new String[] { "160 Rowe St,Eastwood nsw 2122",
				"215 Rowe St,Eastwood nsw 2122",
				"101 Waterlloo Rd,North Ryde NSW 2113", "PO Box 463,Eastwood NSW 2122",
				"PO Box 218,Eastwood NSW 2122" };
	}
}
